﻿namespace MDXStudio.Tracing
{
    using MDXStudio.Utils;
    using Microsoft.AnalysisServices;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    internal class TracerSettingDialog : Form
    {
        private IContainer components;
        private Button mBtnCancel;
        private Button mBtnOk;
        private Button mBtnPropAdd;
        private Button mBtnPropDown;
        private Button mBtnPropRemove;
        private Button mBtnPropUp;
        private Label mLblPropAvailable;
        private Label mLblPropSelected;
        private ListBox mListPropAvailable;
        private ListBox mListPropSelected;
        private CheckedListBox mLstEvents;
        private TracerSetting mSetting;
        private TabControl mTabCtrl;
        private TabPage mTabPageEvents;
        private SplitContainer splitContainer1;
        private TabPage tabPage2;

        private TracerSettingDialog(TracerSetting pSetting)
        {
            this.InitializeComponent();
            this.mSetting = pSetting;
            this.InitTabEvent();
            this.Setting2TabEvent();
            this.Setting2TabProp();
        }

        private void AddProp()
        {
            int selectedIndex = this.mListPropAvailable.SelectedIndex;
            IList selectedItems = this.mListPropAvailable.SelectedItems;
            if (selectedItems.Count > 0)
            {
                List<string> collection = new List<string>();
                for (int i = 0; i < selectedItems.Count; i++)
                {
                    collection.Add((string) selectedItems[i]);
                }
                this.mSetting.PropertyList.AddRange(collection);
                this.Setting2TabProp();
                int count = this.mListPropAvailable.Items.Count;
                if (count > 0)
                {
                    if (selectedIndex == count)
                    {
                        selectedIndex--;
                    }
                    this.mListPropAvailable.SelectedIndex = selectedIndex;
                }
            }
        }

        private bool CheckSetting()
        {
            if (this.mSetting.PropertyList.Count == 0)
            {
                MessageBox.Show(this, "The selected property list could'n be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (this.mSetting.EventTypeList.Count == 0)
            {
                MessageBox.Show(this, "The selected event list could'n be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            return true;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.mTabCtrl = new TabControl();
            this.mTabPageEvents = new TabPage();
            this.mLstEvents = new CheckedListBox();
            this.tabPage2 = new TabPage();
            this.splitContainer1 = new SplitContainer();
            this.mLblPropAvailable = new Label();
            this.mListPropAvailable = new ListBox();
            this.mBtnPropRemove = new Button();
            this.mBtnPropAdd = new Button();
            this.mLblPropSelected = new Label();
            this.mListPropSelected = new ListBox();
            this.mBtnPropUp = new Button();
            this.mBtnPropDown = new Button();
            this.mBtnOk = new Button();
            this.mBtnCancel = new Button();
            this.mTabCtrl.SuspendLayout();
            this.mTabPageEvents.SuspendLayout();
            this.tabPage2.SuspendLayout();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            base.SuspendLayout();
            this.mTabCtrl.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.mTabCtrl.Controls.Add(this.mTabPageEvents);
            this.mTabCtrl.Controls.Add(this.tabPage2);
            this.mTabCtrl.Location = new Point(9, 10);
            this.mTabCtrl.Margin = new Padding(2);
            this.mTabCtrl.Name = "mTabCtrl";
            this.mTabCtrl.SelectedIndex = 0;
            this.mTabCtrl.Size = new Size(0x218, 0x1f7);
            this.mTabCtrl.TabIndex = 0;
            this.mTabPageEvents.Controls.Add(this.mLstEvents);
            this.mTabPageEvents.Location = new Point(4, 0x16);
            this.mTabPageEvents.Margin = new Padding(2);
            this.mTabPageEvents.Name = "mTabPageEvents";
            this.mTabPageEvents.Padding = new Padding(2);
            this.mTabPageEvents.Size = new Size(0x210, 0x1dd);
            this.mTabPageEvents.TabIndex = 0;
            this.mTabPageEvents.Text = "Events";
            this.mTabPageEvents.UseVisualStyleBackColor = true;
            this.mLstEvents.CheckOnClick = true;
            this.mLstEvents.Dock = DockStyle.Fill;
            this.mLstEvents.FormattingEnabled = true;
            this.mLstEvents.Location = new Point(2, 2);
            this.mLstEvents.Margin = new Padding(2);
            this.mLstEvents.Name = "mLstEvents";
            this.mLstEvents.Size = new Size(0x20c, 0x1d5);
            this.mLstEvents.TabIndex = 0;
            this.mLstEvents.SelectedIndexChanged += new EventHandler(this.mLstEvents_SelectedIndexChanged);
            this.mLstEvents.ItemCheck += new ItemCheckEventHandler(this.mLstEvents_ItemCheck);
            this.mLstEvents.Click += new EventHandler(this.mLstEvents_Click);
            this.tabPage2.Controls.Add(this.splitContainer1);
            this.tabPage2.Location = new Point(4, 0x16);
            this.tabPage2.Margin = new Padding(2);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Padding = new Padding(2);
            this.tabPage2.Size = new Size(0x210, 0x1dd);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "Fields";
            this.tabPage2.UseVisualStyleBackColor = true;
            this.splitContainer1.Dock = DockStyle.Fill;
            this.splitContainer1.Location = new Point(2, 2);
            this.splitContainer1.Name = "splitContainer1";
            this.splitContainer1.Panel1.Controls.Add(this.mLblPropAvailable);
            this.splitContainer1.Panel1.Controls.Add(this.mListPropAvailable);
            this.splitContainer1.Panel1.Controls.Add(this.mBtnPropRemove);
            this.splitContainer1.Panel1.Controls.Add(this.mBtnPropAdd);
            this.splitContainer1.Panel2.Controls.Add(this.mLblPropSelected);
            this.splitContainer1.Panel2.Controls.Add(this.mListPropSelected);
            this.splitContainer1.Panel2.Controls.Add(this.mBtnPropUp);
            this.splitContainer1.Panel2.Controls.Add(this.mBtnPropDown);
            this.splitContainer1.Size = new Size(0x20c, 0x1d9);
            this.splitContainer1.SplitterDistance = 0x106;
            this.splitContainer1.TabIndex = 0;
            this.mLblPropAvailable.AutoSize = true;
            this.mLblPropAvailable.ImeMode = ImeMode.NoControl;
            this.mLblPropAvailable.Location = new Point(3, 6);
            this.mLblPropAvailable.Name = "mLblPropAvailable";
            this.mLblPropAvailable.Size = new Size(0x63, 13);
            this.mLblPropAvailable.TabIndex = 0x16;
            this.mLblPropAvailable.Text = "Available attributes:";
            this.mLblPropAvailable.TextAlign = ContentAlignment.MiddleLeft;
            this.mListPropAvailable.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.mListPropAvailable.Location = new Point(3, 0x1a);
            this.mListPropAvailable.Name = "mListPropAvailable";
            this.mListPropAvailable.SelectionMode = SelectionMode.MultiExtended;
            this.mListPropAvailable.Size = new Size(0xde, 0x1be);
            this.mListPropAvailable.Sorted = true;
            this.mListPropAvailable.TabIndex = 0x13;
            this.mListPropAvailable.SelectedIndexChanged += new EventHandler(this.mListPropAvailable_SelectedIndexChanged);
            this.mListPropAvailable.DoubleClick += new EventHandler(this.mListPropAvailable_DoubleClick);
            this.mBtnPropRemove.Anchor = AnchorStyles.Right;
            this.mBtnPropRemove.FlatStyle = FlatStyle.System;
            this.mBtnPropRemove.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold);
            this.mBtnPropRemove.ImeMode = ImeMode.NoControl;
            this.mBtnPropRemove.Location = new Point(0xe7, 250);
            this.mBtnPropRemove.Name = "mBtnPropRemove";
            this.mBtnPropRemove.Size = new Size(0x1c, 0x1c);
            this.mBtnPropRemove.TabIndex = 0x15;
            this.mBtnPropRemove.Text = "<<";
            this.mBtnPropRemove.Click += new EventHandler(this.mBtnPropRemove_Click);
            this.mBtnPropAdd.Anchor = AnchorStyles.Right;
            this.mBtnPropAdd.FlatStyle = FlatStyle.System;
            this.mBtnPropAdd.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold);
            this.mBtnPropAdd.ImeMode = ImeMode.NoControl;
            this.mBtnPropAdd.Location = new Point(0xe7, 0xd8);
            this.mBtnPropAdd.Name = "mBtnPropAdd";
            this.mBtnPropAdd.Size = new Size(0x1c, 0x1c);
            this.mBtnPropAdd.TabIndex = 20;
            this.mBtnPropAdd.Text = ">>";
            this.mBtnPropAdd.Click += new EventHandler(this.mBtnPropAdd_Click);
            this.mLblPropSelected.AutoSize = true;
            this.mLblPropSelected.ImeMode = ImeMode.NoControl;
            this.mLblPropSelected.Location = new Point(3, 6);
            this.mLblPropSelected.Name = "mLblPropSelected";
            this.mLblPropSelected.Size = new Size(0x62, 13);
            this.mLblPropSelected.TabIndex = 0x17;
            this.mLblPropSelected.Text = "Selected attributes:";
            this.mLblPropSelected.TextAlign = ContentAlignment.MiddleLeft;
            this.mListPropSelected.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.mListPropSelected.Location = new Point(3, 0x1a);
            this.mListPropSelected.Name = "mListPropSelected";
            this.mListPropSelected.SelectionMode = SelectionMode.MultiExtended;
            this.mListPropSelected.Size = new Size(0xda, 0x1be);
            this.mListPropSelected.TabIndex = 0x16;
            this.mListPropSelected.SelectedIndexChanged += new EventHandler(this.mListPropSelected_SelectedIndexChanged);
            this.mListPropSelected.DoubleClick += new EventHandler(this.mListPropSelected_DoubleClick);
            this.mBtnPropUp.Anchor = AnchorStyles.Right;
            this.mBtnPropUp.FlatStyle = FlatStyle.System;
            this.mBtnPropUp.Font = new Font("Wingdings", 8.25f, FontStyle.Bold);
            this.mBtnPropUp.ImeMode = ImeMode.NoControl;
            this.mBtnPropUp.Location = new Point(0xe3, 0xd8);
            this.mBtnPropUp.Name = "mBtnPropUp";
            this.mBtnPropUp.Size = new Size(0x1c, 0x1c);
            this.mBtnPropUp.TabIndex = 20;
            this.mBtnPropUp.Text = "\x00e9";
            this.mBtnPropUp.Click += new EventHandler(this.mBtnPropUp_Click);
            this.mBtnPropDown.Anchor = AnchorStyles.Right;
            this.mBtnPropDown.FlatStyle = FlatStyle.System;
            this.mBtnPropDown.Font = new Font("Wingdings", 8.25f, FontStyle.Bold);
            this.mBtnPropDown.ImeMode = ImeMode.NoControl;
            this.mBtnPropDown.Location = new Point(0xe3, 250);
            this.mBtnPropDown.Name = "mBtnPropDown";
            this.mBtnPropDown.Size = new Size(0x1c, 0x1c);
            this.mBtnPropDown.TabIndex = 0x15;
            this.mBtnPropDown.Text = "\x00ea";
            this.mBtnPropDown.Click += new EventHandler(this.mBtnPropDown_Click);
            this.mBtnOk.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.mBtnOk.Location = new Point(0x1ac, 0x210);
            this.mBtnOk.Margin = new Padding(2);
            this.mBtnOk.Name = "mBtnOk";
            this.mBtnOk.Size = new Size(0x38, 0x13);
            this.mBtnOk.TabIndex = 1;
            this.mBtnOk.Text = "OK";
            this.mBtnOk.UseVisualStyleBackColor = true;
            this.mBtnOk.Click += new EventHandler(this.mBtnOk_Click);
            this.mBtnCancel.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.mBtnCancel.Location = new Point(0x1e9, 0x210);
            this.mBtnCancel.Margin = new Padding(2);
            this.mBtnCancel.Name = "mBtnCancel";
            this.mBtnCancel.Size = new Size(0x38, 0x13);
            this.mBtnCancel.TabIndex = 2;
            this.mBtnCancel.Text = "Cancel";
            this.mBtnCancel.UseVisualStyleBackColor = true;
            this.mBtnCancel.Click += new EventHandler(this.mBtnCancel_Click);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x22a, 0x22d);
            base.Controls.Add(this.mBtnCancel);
            base.Controls.Add(this.mBtnOk);
            base.Controls.Add(this.mTabCtrl);
            base.Margin = new Padding(2);
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "TracerSettingDialog";
            this.Text = "TracerSettingDialog";
            this.mTabCtrl.ResumeLayout(false);
            this.mTabPageEvents.ResumeLayout(false);
            this.tabPage2.ResumeLayout(false);
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel1.PerformLayout();
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.Panel2.PerformLayout();
            this.splitContainer1.ResumeLayout(false);
            base.ResumeLayout(false);
        }

        private void InitTabEvent()
        {
            List<TraceEventClass> eventTypeList = TracerSetting.GetFullSetting().EventTypeList;
            this.mLstEvents.Items.Clear();
            foreach (TraceEventClass class2 in eventTypeList)
            {
                this.mLstEvents.Items.Add(class2);
            }
        }

        private void mBtnCancel_Click(object sender, EventArgs e)
        {
            base.Hide();
            base.DialogResult = DialogResult.Cancel;
        }

        private void mBtnOk_Click(object sender, EventArgs e)
        {
            if (this.CheckSetting())
            {
                base.Hide();
                base.DialogResult = DialogResult.OK;
            }
        }

        private void mBtnPropAdd_Click(object sender, EventArgs e)
        {
            try
            {
                this.AddProp();
            }
            catch (Exception exception)
            {
                UIUtil.MessageError(this, exception);
            }
        }

        private void mBtnPropDown_Click(object sender, EventArgs e)
        {
            try
            {
                int selectedIndex = this.mListPropSelected.SelectedIndex;
                if ((selectedIndex >= 0) && (selectedIndex < (this.mListPropSelected.Items.Count - 1)))
                {
                    this.mSetting.PropertyList.Reverse(selectedIndex, 2);
                    this.Setting2TabProp();
                    this.mListPropSelected.SelectedIndex = selectedIndex + 1;
                }
            }
            catch (Exception exception)
            {
                UIUtil.MessageError(this, exception);
            }
        }

        private void mBtnPropRemove_Click(object sender, EventArgs e)
        {
            try
            {
                this.RemoveProp();
            }
            catch (Exception exception)
            {
                UIUtil.MessageError(this, exception);
            }
        }

        private void mBtnPropUp_Click(object sender, EventArgs e)
        {
            try
            {
                int selectedIndex = this.mListPropSelected.SelectedIndex;
                if ((selectedIndex > 0) && (selectedIndex < this.mListPropSelected.Items.Count))
                {
                    this.mSetting.PropertyList.Reverse(selectedIndex - 1, 2);
                    this.Setting2TabProp();
                    this.mListPropSelected.SelectedIndex = selectedIndex - 1;
                }
            }
            catch (Exception exception)
            {
                UIUtil.MessageError(this, exception);
            }
        }

        private void mListPropAvailable_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                this.AddProp();
            }
            catch (Exception exception)
            {
                UIUtil.MessageError(this, exception);
            }
        }

        private void mListPropAvailable_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.mBtnPropAdd.Enabled = this.mListPropAvailable.SelectedItems.Count > 0;
        }

        private void mListPropSelected_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                this.RemoveProp();
            }
            catch (Exception exception)
            {
                UIUtil.MessageError(this, exception);
            }
        }

        private void mListPropSelected_SelectedIndexChanged(object sender, EventArgs e)
        {
            int count = this.mListPropSelected.SelectedItems.Count;
            this.mBtnPropRemove.Enabled = count > 0;
            this.mBtnPropUp.Enabled = count == 1;
            this.mBtnPropDown.Enabled = count == 1;
        }

        private void mLstEvents_Click(object sender, EventArgs e)
        {
        }

        private void mLstEvents_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            int index = e.Index;
            TraceEventClass item = (TraceEventClass) this.mLstEvents.Items[index];
            if (this.mSetting.EventTypeList.Contains(item))
            {
                if (e.NewValue == CheckState.Unchecked)
                {
                    this.mSetting.EventTypeList.Remove(item);
                }
            }
            else if (e.NewValue == CheckState.Checked)
            {
                this.mSetting.EventTypeList.Add(item);
            }
        }

        private void mLstEvents_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void RemoveProp()
        {
            int selectedIndex = this.mListPropSelected.SelectedIndex;
            IList selectedItems = this.mListPropSelected.SelectedItems;
            if (selectedItems.Count > 0)
            {
                List<string> list2 = new List<string>();
                for (int i = 0; i < selectedItems.Count; i++)
                {
                    list2.Add((string) selectedItems[i]);
                }
                UIUtil.RemoveList<string>(this.mSetting.PropertyList, list2);
                this.Setting2TabProp();
                int count = this.mListPropSelected.Items.Count;
                if (count > 0)
                {
                    if (selectedIndex == count)
                    {
                        selectedIndex--;
                    }
                    this.mListPropSelected.SelectedIndex = selectedIndex;
                }
            }
        }

        private void Setting2TabEvent()
        {
            List<TraceEventClass> eventTypeList = this.mSetting.EventTypeList;
            for (int i = 0; i < this.mLstEvents.Items.Count; i++)
            {
                TraceEventClass item = (TraceEventClass) this.mLstEvents.Items[i];
                this.mLstEvents.SetItemChecked(i, eventTypeList.Contains(item));
            }
        }

        private void Setting2TabProp()
        {
            List<string> list2 = new List<string>(TracerSetting.GetFullSetting().PropertyList);
            List<string> propertyList = this.mSetting.PropertyList;
            bool flag = list2.Count > 0;
            this.mLblPropAvailable.Enabled = flag;
            this.mLblPropSelected.Enabled = flag;
            this.mListPropAvailable.Enabled = flag;
            this.mListPropSelected.Enabled = flag;
            UIUtil.RemoveList<string>(list2, propertyList);
            this.mListPropAvailable.BeginUpdate();
            this.mListPropSelected.BeginUpdate();
            this.mListPropSelected.Items.Clear();
            for (int i = 0; i < propertyList.Count; i++)
            {
                this.mListPropSelected.Items.Add(propertyList[i]);
            }
            this.mListPropAvailable.Items.Clear();
            this.mListPropAvailable.Sorted = false;
            for (int j = 0; j < list2.Count; j++)
            {
                this.mListPropAvailable.Items.Add(list2[j]);
            }
            this.mListPropAvailable.Sorted = true;
            this.mListPropAvailable.EndUpdate();
            this.mListPropSelected.EndUpdate();
            this.mBtnPropAdd.Enabled = flag && (this.mListPropAvailable.SelectedItems.Count > 0);
            this.mBtnPropRemove.Enabled = flag && (this.mListPropSelected.SelectedItems.Count > 0);
            this.mBtnPropUp.Enabled = flag && (this.mListPropSelected.SelectedItems.Count == 1);
            this.mBtnPropDown.Enabled = flag && (this.mListPropSelected.SelectedItems.Count == 1);
        }

        public static bool StartDialog(IWin32Window pOwner, TracerSetting pSetting)
        {
            bool flag;
            TracerSettingDialog dialog = new TracerSettingDialog(pSetting);
            try
            {
                if (dialog.ShowDialog(pOwner) == DialogResult.OK)
                {
                    return true;
                }
                flag = false;
            }
            finally
            {
                dialog.Close();
            }
            return flag;
        }
    }
}

